# !/usr/bin/python
# -*- coding: utf-8 -*-

# mediaplex
# multimedia platform
#
# this file is licensed under the MIT license
# read "LICENSE.MIT" in the root of this distribution

__authors__ = 'Jan Jokela <janjokela@gmail.com>'
__description__ = 'Media objects'

import mmpython

class Audio:
    """
    The class that represents all kinds of audio objects.

    To be noted, is the attributes present, which represent the
    lowest common denominator found in most files of this type
    (like mp3, ogg/vorbis, wma, etc.).
    """
    def __init__(self, uri, length=0, size=0,
                 media_type='audio', mime_type='', track_number=0,
                 title='', artist='', album='', genre='', year=0, album_art=''):
        self.uri = uri    # file location in the filesystem
        self.length = length    # The length in seconds
        self.size = size       # The size in bytes
        self.media_type = media_type    
        self.mime_type = mime_type
        self.track_number = track_number
        self.title = title
        self.artist = artist
        self.album = album
        self.genre = genre
        self.year = year
        self.album_art = album_art # album art location

    def update_metadata(self, metadata=None):
        """
        Updates the metadata concerning this object.

        This procedure is not done automatically to prevent excessive
        delays when performing massive operations. Imagine creating
        a playlist for all the musics of genre "classical". The user
        might have several gigabytes of it, and gathering the metadata
        at the same time it tries to create objects from this class and
        sort them all out, would be too high for common use.

        We allow the user to pass the "metadata" object, in case it
        has already done so to find, for example, the mimetype. This
        will avoid the overhead of calling the method twice.

        :metadata (mmpython.parse()) -- The metadata to populate.
        """
        if metadata == None:
            metadata = mmpython.parse(self.uri)
        self.length = metadata.length
        self.size = metadata.size
        self.mime_type = metadata.mime
        self.track_number = metadata.trackno
        self.title = metadata.title
        self.artist = metadata.artist
        self.album = metadata.album
        self.genre = metadata.genre
        self.year = metadata.date

        if self.title == '' or self.title == 'None' or self.title == 'Unknown' or self.title == '':
            self.title = None
        if self.artist == '' or self.artist == 'None' or self.artist == 'Unknown' or self.artist == '':
            self.artist = None
        if self.album == '' or self.album == 'None' or self.album == 'Unknown' or self.album == '':
            self.album = None
        if self.genre == '' or self.genre == 'None' or self.genre == 'Unknown' or self.genre == '':
            self.genre = None



class Video:
    """
    The class that represents all kinds of video objects.

    To be noted, is the attributes present, which represent the
    lowest common denominator found in most files of this type
    (like avi, ogg/theora, wmv, etc.).
    """
    def __init__(self, uri, length=0, size=0, 
                media_type='video', mime_type='', title=''):
        self.uri = uri
        self.length = length    # The length in seconds
        self.size = size       # The size in bytes
        self.media_type = media_type
        self.title = title
        self.mime_type = mime_type

    def update_metadata(self, metadata=None):
        """Updates the metadata concerning this object.

        This procedure is not done automatically to prevent excessive
        delays when performing massive operations. Imagine creating
        a playlist for all the musics of genre "classical". The user
        might have several gigabytes of it, and gathering the metadata
        at the same time it tries to create objects from this class and
        sort them all out, would be too high for common use.

        We allow the user to pass the "metadata" object, in case it
        has already done so to find, for example, the mimetype. This
        will avoid the overhead of calling the method twice.

        :metadata (mmpython.parse()) -- The metadata to populate.
        """
        if metadata == None:
            metadata = mmpython.parse(self.uri)
        self.length = metadata.length
        self.size = metadata.size
        self.title = metadata.title
        self.mime_type = metadata.mime

class Image:
    """
    The class that represents all kinds of image objects.

    To be noted, is the attributes present, which represent the
    lowest common denominator found in most files of this type
    (like png, jpg, etc.).
    """
    def __init__(self, uri, length=0, size=0, 
                media_type='image', mime_type='', title='',
                width=0, height=0):
        self.uri = uri
        self.size = size       # The size in bytes
        self.media_type = media_type
        self.title = title
        self.mime_type = mime_type
        self.width = width
        self.height = height

    def update_metadata(self, metadata=None):
        """
        Updates the metadata concerning this object.

        This procedure is not done automatically to prevent excessive
        delays when performing massive operations. Imagine creating
        a playlist for all the musics of genre "classical". The user
        might have several gigabytes of it, and gathering the metadata
        at the same time it tries to create objects from this class and
        sort them all out, would be too high for common use.

        We allow the user to pass the "metadata" object, in case it
        has already done so to find, for example, the mimetype. This
        will avoid the overhead of calling the method twice.

        :metadata (mmpython.parse()) -- The metadata to populate.
        """
        if metadata == None:
            metadata = mmpython.parse(self.uri)
        self.size = metadata.size
        self.title = metadata.title
        self.mime_type = metadata.mime
        self.width = metadata.width
        self.height = metadata.height
